package cn.gcyl.module.biz.service.columntype;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.gcyl.module.biz.controller.admin.columntype.vo.*;
import cn.gcyl.module.biz.dal.dataobject.columntype.ColumnTypeDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.columntype.ColumnTypeMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.COLUMN_TYPE_NOT_EXISTS;

/**
 * 栏目类型 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ColumnTypeServiceImpl implements ColumnTypeService {

    @Resource
    private ColumnTypeMapper columnTypeMapper;

    @Override
    public Integer createColumnType(ColumnTypeSaveReqVO createReqVO) {
        // 插入
        ColumnTypeDO columnType = BeanUtils.toBean(createReqVO, ColumnTypeDO.class);
        columnTypeMapper.insert(columnType);

        // 返回
        return columnType.getId();
    }

    @Override
    public void updateColumnType(ColumnTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateColumnTypeExists(updateReqVO.getId());
        // 更新
        ColumnTypeDO updateObj = BeanUtils.toBean(updateReqVO, ColumnTypeDO.class);
        columnTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteColumnType(Integer id) {
        // 校验存在
        validateColumnTypeExists(id);
        // 删除
        columnTypeMapper.deleteById(id);
    }

    @Override
        public void deleteColumnTypeListByIds(List<Integer> ids) {
        // 删除
        columnTypeMapper.deleteByIds(ids);
        }


    private void validateColumnTypeExists(Integer id) {
        if (columnTypeMapper.selectById(id) == null) {
            throw exception(COLUMN_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ColumnTypeDO getColumnType(Integer id) {
        return columnTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ColumnTypeDO> getColumnTypePage(ColumnTypePageReqVO pageReqVO) {
        return columnTypeMapper.selectPage(pageReqVO);
    }
    @Override
    public List<ColumnTypeDO> getAppColumnTypeList(ColumnTypePageReqVO pageReqVO) {
        return columnTypeMapper.getAppColumnTypeList(pageReqVO);
    }

}